﻿#region

using DomainModel.Domain.Model.ServiceModel;
using FluentNHibernate.Mapping;

#endregion

namespace DomainModel.Infrastructure.Mappings
{
    public class ServiceMap : ClassMap<Service>
    {
        public ServiceMap()
        {
            CompositeId(x => x.Id).KeyProperty(x => x.Id);

            Component(x => x.CreateModifyInfo);

            Map(x => x.FirstSeen);
            Component(x => x.InputQueue).ColumnPrefix("inputQueue_");
            Component(x => x.ErrorQueue).ColumnPrefix("errorQueue_"); ;
            Component(x => x.AuditQueue).ColumnPrefix("auditQueue_"); ;
            Map(x => x.LastAlive);
            Map(x => x.LastStarted);
            Map(x => x.Name);
            Map(x => x.Summary);
            Map(x => x.Version);

            HasMany(x => x.ServiceRolesSet)
                .Cascade.AllDeleteOrphan().Inverse();

            HasManyToMany(x => x.MessageAssembliesSet)
                .Cascade.None()
                // ten cascade nie jest raczej potrzebny, powoduje blad "Illegal attempt to associate a collection with two open sessions" przy Store() na istniejacym obiekcie service - do zbadania
                .Table("ServiceMessageAssembly");
        }
    }

    public class ServiceRoleMap : ClassMap<ServiceRole>
    {
        public ServiceRoleMap()
        {
            CompositeId(x => x.Id).KeyProperty(x => x.Id);

            Component(x => x.CreateModifyInfo);

            References(x => x.Service);

            DiscriminateSubClassesOnColumn<string>("ServiceRoleType")
                .SubClass<HandlerRole>("Handler", m => m.References(x=>x.HandledType))
                .SubClass<PublisherRole>("Publisher", m => { })
                .SubClass<SubscriberRole>("Subscriber", m => m.References(x => x.Subscription));
        }
    }
}